// 处理用户注册逻辑
const db = require("../lib/db")

function addUser(username,userpwd,desc){
    return new Promise((resolve , reject)=>{
        let sql =  `insert into user (userName, userPassword,createTime, headImg ,adesc) values ('${username}', '${userpwd}', '${new Date()}', 'default.jpg', '${desc}')`
        db.querySql(sql);
        let sql1 = `select * from user where userName ='${username}'`
        db.querySql(sql1).then(data =>{
            if(data.length == 0){
                // 用户不存在
                resolve(404)
            }else{
               let arr=[]
                data.map(element=>{
                    arr.push({
                        _id: Buffer.from(String(element.userId)).toString("Base64"),
                        userName: element.userName,
                        password:element.userPassword,
                        createTime: element.createTime, 
                        headImg: element.headImg, 
                        desc: element.adesc
                    })
                })
                console.log(arr)
                resolve(arr)
            }
    })
    
    })
}
// 导出
module.exports = addUser;